package com.young.common.config;

import com.young.common.client.ums.UmsClientInterceptor;
import com.young.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * webmvc统一配置处理
 * @author imrookie
 * @date 2018/10/1
 */
@Component
@ConfigurationProperties(prefix="young.interceptor")
public class YoungWebMvcConfig extends WebMvcConfigurationSupport {

    Logger logger = LoggerFactory.getLogger(YoungWebMvcConfig.class);

    @Resource
    UmsClientInterceptor umsClientInterceptor;

    @Resource
    DateFormatter dateFormatter;

    //拦截器的bean,多个使用逗号拼接
    private String beanNames;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        if (StringUtils.isBlank(beanNames)){
            logger.info("[YoungWebMvc全局配置-拦截器] 未发现拦截器bean配置,若需开启拦截器,请配置[young.interceptor.beanNames],多个拦截器bean使用逗号拼接");
        }

        String[] beanNameArr = beanNames.split(",");
        if (beanNameArr == null || beanNameArr.length == 0){
            logger.error("[YoungWebMvc全局配置-拦截器] 参数[young.interceptor.beanNames={}]解析为数组失败", beanNames);
        }
        super.addInterceptors(registry);

        //遍历,装载拦截器配置
        for (String beanName : beanNameArr){
            if (StringUtils.isNotBlank(beanName)){
                //根据配置获取bean
                Object interceptor = SpringContextHolder.getBean(beanName);
                if (interceptor == null){
                    logger.warn("[YoungWebMvc全局配置-拦截器] 根据beanName={}获取bean实例失败", beanName);
                    continue;
                }

                if (interceptor instanceof ICommonInterceptor){//公共拦截器接口
                    //拦截器自行注册
                    ((ICommonInterceptor)interceptor).addInterceptors(registry);
                } else if (interceptor instanceof HandlerInterceptor){//spring mvc原始拦截器接口
                    //注册拦截器,默认url模式为/*,默认排除static和page下的静态文件
                    registry.addInterceptor((HandlerInterceptor)interceptor).addPathPatterns("/**").excludePathPatterns("/static/**", "/page/**");
                    logger.info("[YoungWebMvc全局配置-拦截器] 拦截器[{}]注册完成.", beanName);
                } else {
                    logger.warn("[YoungWebMvc全局配置-拦截器] 未知的拦截器实例类型:{},目前支持类型为:HandlerInterceptor,ICommonInterceptor", interceptor.getClass().getName());
                }
            }else {
                logger.warn("[YoungWebMvc全局配置-拦截器] bean设置中存在beanName为空:[young.interceptor.beanNames={}]", beanNames);
            }
        }
    }

    /**
     * 注册静态资源
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //第一个方法设置访问路径前缀，第二个方法设置资源路径
        //静态资源
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        //页面
        registry.addResourceHandler("/page/**").addResourceLocations("classpath:/page/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/");
    }

    /**
     * 格式化
     * @param registry
     */
    @Override
    protected void addFormatters(FormatterRegistry registry) {
        //时间格式化
        registry.addFormatter(dateFormatter);
    }

    public String getBeanNames() {
        return beanNames;
    }

    public void setBeanNames(String beanNames) {
        this.beanNames = beanNames;
    }
}
